<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header :addNewTodo="addNewTodo" />
      <List
        :todoList="todoList"
        :deleteTodo="deleteTodo"
        :changeDone="changeDone"
      />
      <Footer
        :checkAll="checkAll"
        :todoList="todoList"
        :clearcompletedtasks="clearcompletedtasks"
      />
    </div>
  </div>
</template>

<script>
//组件引入
import Header from "@/components/Header/Header";
import Footer from "@/components/Footer/Footer";
import List from "@/components/List/List";

//npm包 uuid
import { v4 as uuidv4 } from "uuid";
export default {
  name: "App",
  components: { Header, Footer, List },
  data() {
    return {
      todoList: [
        { id: uuidv4(), todo: "唱", done: false },
        { id: uuidv4(), todo: "跳", done: false },
        { id: uuidv4(), todo: "rap", done: false },
      ],
    };
  },
  methods: {
    // 添加单个List
    addNewTodo(newTodo) {
      this.todoList.unshift(newTodo);
    },
    //修改done
    //让data数据和视图保持一致
    changeDone(id) {
      //map映射 将返回的数据放入到新的数组中 不会影响原数组
      this.todoList = this.todoList.map((item) => {
        //如果当前传递过来的id等于item.id 那么就给item.done取反  反之则返回原来的对象
        return item.id === id ? { ...item, done: !item.done } : item;
      });
    },
    //删除单个list
    deleteTodo(id) {
      //confirm()用户做出弹窗 有确定和取消 返回值为布尔值
      const isDelete = confirm("你真的完成任务了吗kunkun~~~");
      //如果用户点取消为false 取反为ture进入if判断 停止删除
      if (!isDelete) return;
      this.todoList = this.todoList.filter((item) => item.id !== id);
    },
    //全选逻辑
    checkAll(value) {
      //拿到选框的值修改数组中所有done的值
      this.todoList = this.todoList.map((item) => {
        return { ...item, done: value };
      });
    },
    //清除已完成任务
    clearcompletedtasks() {
      //这里不需要参数 直接把事件函数传递给footer组件 调用即可
      //进行一个过滤筛选
      this.todoList = this.todoList.filter((item) => {
        //如果当done不等等于ture 则返回ture 证明是符合条件的留下
        return item.done !== true;
      });
    },
  },
};
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
